package com.iot.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iot.commonutils.utils.Result;
import com.iot.user.entity.User;
import com.iot.user.mapper.UserMapper;
import com.iot.user.service.UpdateUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UpdateUserInfoServiceImpl extends ServiceImpl<UserMapper, User> implements UpdateUserInfoService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Result updateUserInfo(User user) {
        //昵称和头像不能为空
        if (StringUtils.isEmpty(user.getNickname()) && StringUtils.isEmpty(user.getImage())) {
            return Result.error("昵称或者头像不能为空");
        }
        String user_id = redisTemplate.opsForValue().get("USER_ID");
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", user_id);
        update(user, wrapper);
        return Result.success("用户信息修改成功!");
    }
}
